<template>
  <div class="main">
    <h3>欢迎登录</h3>
    <h3>薪启航个税服务平台</h3>
    <a-form-model class="user-layout-login" @keyup.enter.native="handleSubmit">
      <!-- <a-tabs
        :activeKey="customActiveKey"
        :tabBarStyle="{ textAlign: 'center', borderBottom: 'unset' }"
        @change="handleTabClick"
      >
        <a-tab-pane key="tab1" tab="账号密码登录">
          <login-account
            ref="alogin"
            @validateFail="validateFail"
            @success="requestSuccess"
            @fail="requestFailed"
          ></login-account>
        </a-tab-pane>

        <a-tab-pane key="tab2" tab="手机号登录">
          <login-phone
            ref="plogin"
            @validateFail="validateFail"
            @success="requestSuccess"
            @fail="requestFailed"
          ></login-phone>
        </a-tab-pane>
      </a-tabs> -->

      <login-account ref="alogin" @validateFail="validateFail" @success="requestSuccess" @fail="requestFailed"></login-account>

      <!-- <a-form-model-item>
        <a-checkbox @change="handleRememberMeChange" default-checked>记住密码</a-checkbox>
        <router-link :to="{ name: 'alteration' }" class="forge-password" style="float: right;">
          忘记密码
        </router-link>
        <router-link :to="{ name: 'register' }" class="forge-password" style="float: right;margin-right: 10px">
          注册账户
        </router-link>
      </a-form-model-item> -->
      <div class="forgetMm">忘记密码</div>
      <a-form-item style="margin-top:24px">
        <a-button
          size="large"
          type="primary"
          htmlType="submit"
          class="login-button"
          :loading="loginBtn"
          @click.stop.prevent="handleSubmit"
          :disabled="loginBtn">登录</a-button>
      </a-form-item>
      <div class="tipsInfo"><a-icon class="tip-left-icon" type="exclamation-circle" />没有账号或忘记密码？请联系平台管理员</div>
      <!-- <a-divider>你可以选择以下方式登录</a-divider>
      <div class="wxLogin-wrap">
        <a-icon class="weixinLogin" type="wechat" @click="wxLogin()" />
        <div v-show="wxVisible" class="wxlogin-box">
          <div class="bottom-arrow"></div>
          <a-icon class="wxClose" type="close" @click="closeWxBox" />
          <div id="wx_login_container"></div>
        </div>

      </div> -->
    </a-form-model>

    <!-- <two-step-captcha
      v-if="requiredTwoStepCaptcha"
      :visible="stepCaptchaVisible"
      @success="stepCaptchaSuccess"
      @cancel="stepCaptchaCancel"
    ></two-step-captcha> -->
    <login-select-tenant ref="loginSelect" @success="loginSelectOk"></login-select-tenant>
    <!-- <third-login ref="thirdLogin"></third-login> -->
  </div>
</template>

<script>
import Vue from 'vue'
import { ACCESS_TOKEN, ENCRYPTED_STRING } from '@/store/mutation-types'
import ThirdLogin from './third/ThirdLogin'
import LoginSelectTenant from './LoginSelectTenant'
import TwoStepCaptcha from '@/components/tools/TwoStepCaptcha'
import { encryption, getEncryptedString } from '@/utils/encryption/aesEncrypt'
import { timeFix } from '@/utils/util'

import LoginAccount from './LoginAccount'
import LoginPhone from './LoginPhone'

export default {
  components: {
    LoginSelectTenant,
    TwoStepCaptcha,
    ThirdLogin,
    LoginAccount,
    LoginPhone
  },
  data() {
    return {
      wxVisible: false,
      customActiveKey: 'tab1',
      rememberMe: false,
      loginBtn: false,
      requiredTwoStepCaptcha: false,
      stepCaptchaVisible: false,
      encryptedString: {
        key: '',
        iv: ''
      }
    }
  },
  created() {
    Vue.ls.remove(ACCESS_TOKEN)
    this.getRouterData()
    this.rememberMe = false
    this.getEncrypte()
  },
  mounted() {
    //this.get_wx_qrcode() // 获取微信的登录二维码
  },
  methods: {
    handleTabClick(key) {
      this.customActiveKey = key
    },
    handleRememberMeChange(e) {
      this.rememberMe = e.target.checked
    },
    /** 跳转到登录页面的参数-账号获取 */
    getRouterData() {
      this.$nextTick(() => {
        let temp = this.$route.params.username || this.$route.query.username || ''
        if (temp) {
          this.$refs.alogin.acceptUsername(temp)
        }
      })
    },

    // 登录
    handleSubmit() {
      this.loginBtn = true

      this.$refs.alogin.handleLogin(this.rememberMe, this.encryptedString)
      // if (this.customActiveKey === 'tab1') {
      //   // 使用账户密码登录
      //   this.$refs.alogin.handleLogin(this.rememberMe)
      // } else {
      //   //手机号码登录
      //   this.$refs.plogin.handleLogin(this.rememberMe)
      // }
    },
    // 校验失败
    validateFail() {
      this.loginBtn = false
    },
    wxLogin() {
      this.wxVisible = !this.wxVisible
    },
    closeWxBox() {
      this.wxVisible = false
    },
    get_wx_qrcode() {
      var obj = new WxLogin({
        self_redirect: false, // 扫码后默认重新打开的回调地址，不是当前页打开
        id: 'wx_login_container',
        appid: 'wxd7438eed48c55c18',
        scope: 'snsapi_login',
        redirect_uri: encodeURIComponent(
          'https://abgq.metouchscreen.cn/loginSuccess' // 配置自己申请的回调地址
        ),
        // redirect_uri: encodeURIComponent('http://10.10.1.163/user/loginSuccess'),
        state: Math.ceil(Math.random() * 1000),
        style: 'black',
        href: ''
      })
    },
    // 登录后台成功
    requestSuccess(loginResult) {
      this.$refs.loginSelect.show(loginResult)
    },
    // 登录后台失败
    requestFailed(err) {
      let description = ((err.response || {}).data || {}).message || err.message || '请求出现错误，请稍后再试'
      this.$notification['error']({
        message: '登录失败',
        description: description,
        duration: 4
      })
      // 账户密码登录错误后更新验证码
      if (this.customActiveKey === 'tab1' && description.indexOf('密码错误') > 0) {
        this.$refs.alogin.handleChangeCheckCode()
      }
      this.loginBtn = false
    },
    loginSelectOk() {
      this.loginSuccess()
    },
    // 登录成功
    loginSuccess() {
      let _this = this
      this.$nextTick(() => {
        setTimeout(res => {
          _this.$router.push({ name: 'dashboard' }).catch(() => {
            console.log('登录跳转首页出错,这个错误从哪里来的')
          })
          _this.$notification.success({
            message: '欢迎',
            description: `${timeFix()}，欢迎回来`
          })
        }, 1000)
      })
    },

    stepCaptchaSuccess() {
      this.loginSuccess()
    },
    stepCaptchaCancel() {
      this.Logout().then(() => {
        this.loginBtn = false
        this.stepCaptchaVisible = false
      })
    },
    // 获取密码加密规则
    getEncrypte() {
      let _this = this
      var encryptedString = Vue.ls.get(ENCRYPTED_STRING)
      console.log('encryptedString:::', encryptedString)
      if (encryptedString == null) {
        getEncryptedString().then(data => {
          _this.encryptedString = data
        })
      } else {
        this.encryptedString = encryptedString
      }
    }

  }
}
</script>
<style lang="less" scoped>
.main {
  h3 {

    margin-bottom: 0px;
    font-size: 36px;
    font-weight: 600;
    color: #333;
    text-align: left;
  }
}
.user-layout-login {
  label {
    font-size: 14px;
  }
  .getCaptcha {
    display: block;
    width: 100%;
    height: 40px;
  }

  .forge-password {
    font-size: 14px;
  }
  .forgetMm {
    text-align: right;
    font-size: 14px;
    font-weight: 500;
    color: #666666;
    cursor: pointer;
  }
  .tipsInfo {
    .tip-left-icon{
      padding-right: 4px;
    }
    font-size: 14px;
    font-weight: 400;
    color: #73848F;
  }

  button.login-button {
    padding: 0 15px;
    font-size: 16px;
    height: 40px;
    width: 432px;
  }

  .user-login-other {
    text-align: left;
    margin-top: 24px;
    line-height: 22px;

    .item-icon {
      font-size: 24px;
      color: rgba(0, 0, 0, 0.2);
      margin-left: 16px;
      vertical-align: middle;
      cursor: pointer;
      transition: color 0.3s;

      &:hover {
        color: #1890ff;
      }
    }

    .register {
      float: right;
    }
  }
  .ant-divider-horizontal.ant-divider-with-text-center {
    padding: 0 45px 0 35px;
    font-size: 12px !important;
    color: #717576 !important;
    font-weight: 400 !important;
  }
  .wxLogin-wrap {
    padding: 0 45px 0 35px;
    .weixinLogin {
      margin: 0 auto;
      display: block;
      width: 40px;
      font-size: 40px;
      color: #07c160;
    }
  }
}
.wx_login_container {
  height: 300px;
}
.valid-error .ant-select-selection__placeholder {
  color: #f5222d;
}

.wxlogin-box {
  background: white;
  text-align: center;
  padding: 20px;
  position: absolute;
  left: 50%;
  margin-left: -175px;
  bottom: 118px;
  box-shadow: 2px 2px 15px 0px #989898;
  .bottom-arrow {
    position: absolute;
    display: block;
    width: 20px;
    height: 20px;
    background: transparent;
    border-style: solid;
    border-width: 10px;
    transform: rotate(45deg);
    bottom: -10px;
    border-top-color: transparent;
    border-right-color: #fff;
    border-bottom-color: #fff;
    border-left-color: transparent;
    box-shadow: 7px 8px 8px 0px rgba(152, 152, 152, 0.23);
    left: 50%;
    margin-left: -12px;
  }
  .wxClose {
    position: absolute;
    right: 10px;
    top: 10px;
    font-size: 14px;
    color: #b4b2b5;
    &:hover {
      color: #3477FF;
    }
  }
}
</style>
